<?php

namespace App\Repositories\System;


use App\Utils\Output;
use App\Utils\TreeUtil;

class PermissionRepository
{

    /**
     * @param $permissionCodes
     * @param $checkedPermissionCodes
     * @return Output
     */
    function queryAllPermission($permissionCodes, $checkedPermissionCodes)
    {
        $permissions = __('permission');
        if (is_array($permissionCodes)) {
            $permissionCodes = array_flip($permissionCodes);
        }
        $checkedPermissionCodes = array_flip($checkedPermissionCodes);
        return Output::data(TreeUtil::toFormatName($this->markPermission($permissions, $permissionCodes, $checkedPermissionCodes)));
    }

    function permissions($permissionCodes, $parentPermissionCodes = false)
    {
        if (is_array($parentPermissionCodes)) {
            $parentPermissionCodes = array_flip($parentPermissionCodes);
            foreach ($permissionCodes as $i => $code) {
                if (!isset($parentPermissionCodes[$code])) {
                    unset($permissionCodes[$i]);
                }
            }
        }
        return array_values($permissionCodes);
    }

    function markPermission($permissions, $permissionCodes, $checkedPermissionCodes)
    {
        $_permissions = [];
        foreach ($permissions as $permission) {
            if (isset($permission['actions'])) {
                foreach ($permission['actions'] as $action => $name) {
                    $code = $permission['controller'] . '@' . $action;
                    if (is_array($permissionCodes) && !isset($permissionCodes[$code])) {
                        unset($permission['actions'][$action]);
                    } else {
                        if (is_string($name)) {
                            $name = [
                                'name' => $name
                            ];
                        }
                        $name['checked'] = isset($checkedPermissionCodes[$code]);
                        $permission['actions'][$action] = $name;
                    }
                }
                if (count($permission['actions'])) {
                    $_permissions[] = $permission;
                }
            } else if (isset($permission['child'])) {
                $child = $this->markPermission($permission['child'], $permissionCodes, $checkedPermissionCodes);
                if (count($child)) {
                    unset($permission['_child']);
                    $permission['_child'] = $child;
                    $_permissions[] = $permission;
                }
            }
        }
        return $_permissions;
    }

    /**
     * 权限差异
     * @param array $oldPermissionIds
     * @param array $newPermissionIds
     * @return array[]
     */
    function permissionDiff($oldPermissionIds, $newPermissionIds)
    {
        $removePermissionIds = [];
        $addPermissionIds = [];
        $permissionIds = array_flip($newPermissionIds);
        foreach ($oldPermissionIds as $permissionId) {
            if (!isset($permissionIds[$permissionId])) {
                $removePermissionIds[] = $permissionId;
            }
        }
        $permissionIds = array_flip($oldPermissionIds);
        foreach ($newPermissionIds as $permissionId) {
            if (!isset($permissionIds[$permissionId])) {
                $addPermissionIds[] = $permissionId;
            }
        }
        return [$addPermissionIds, $removePermissionIds];
    }
}
